package net.yebaihe.buildapp.yyh;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MaintainApp extends Activity {
	
	class MySimpleCursorAdapter extends SimpleCursorAdapter{

		public MySimpleCursorAdapter(Context context, int layout, Cursor c,
				String[] from, int[] to) {
			super(context, layout, c, from, to);
		}
		
		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			Log.d("myown", "cnt:"+cursor.getInt(0));
		    super.bindView(view, context, cursor);
		}
	}
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maintain);
        refreshAppList();
    }

    private void refreshAppList() {
    	MyHelper dbhelper = MyHelper.get(this);
    	SQLiteDatabase db = dbhelper.getReadableDatabase();
    	String sql="select * from app";
    	Cursor result = db.rawQuery(sql, null);
    	CursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.approw,
    			result, new String[] {"cname",},
    			new int[] { R.id.idtitle, });
    	final ListView templist=(ListView) this.findViewById(R.id.applist);
    	templist.setAdapter(adapter);	

    	templist.setOnItemClickListener(new OnItemClickListener(){
    		@Override
    		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    				long arg3) {
    			Cursor row=(Cursor) templist.getItemAtPosition(arg2);
    			Intent i=new Intent(MaintainApp.this,FillContent.class);
    			i.putExtra("appidx", row.getInt(0));
    			startActivity(i);
    		}
    	});
    }
}
